package org.csu.mypetstore.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.csu.mypetstore.api.common.CommonResponse;
import org.csu.mypetstore.api.entity.*;
import org.csu.mypetstore.api.persistence.AccountMapper;
import org.csu.mypetstore.api.persistence.BannerDataMapper;
import org.csu.mypetstore.api.persistence.ProfileMapper;
import org.csu.mypetstore.api.persistence.SignOnMapper;
import org.csu.mypetstore.api.service.AccountService;
import org.csu.mypetstore.api.vo.AccountVO;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("accountService")
@MapperScan("org.csu.mypetstore.api.persistence")
public class AccountServiceImpl implements AccountService {

      @Autowired
      private AccountMapper accountMapper;
      @Autowired
      private BannerDataMapper bannerDataMapper;
      @Autowired
      private ProfileMapper profileMapper;
      @Autowired
      private SignOnMapper signOnMapper;


      @Override
      public CommonResponse<AccountVO> getAccount(String username, String password) {
            QueryWrapper<SignOn> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("username",username);
            queryWrapper.eq("password",password);

            SignOn signOn = signOnMapper.selectOne(queryWrapper);

            if(signOn==null){
                  return CommonResponse.createForError("用户名或密码错误");
            }
            return getAccount(username);
      }

      @Override
      public CommonResponse<AccountVO> getAccountByPhone(String memPhone) {
            QueryWrapper<SignOn> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("phone",memPhone);

            SignOn signOn = signOnMapper.selectOne(queryWrapper);

            if(signOn==null){
                  return CommonResponse.createForError("账户不存在");
            }
            return getAccount(signOn.getUsername());
      }

      @Override
      public CommonResponse<AccountVO> getAccount(String username) {
            Account account = accountMapper.selectById(username);
            Profile profile = profileMapper.selectById(username);
            BannerData bannerData = bannerDataMapper.selectById(profile.getFavouriteCategoryId());
            if(account==null){
                  return  CommonResponse.createForError("获取用户信息失败");
            }

            AccountVO accountVO=entityToVO(account,profile, bannerData);
            return CommonResponse.createForSuccess(accountVO);
      }

      private AccountVO entityToVO(Account account,Profile profile,BannerData bannerData){
            AccountVO accountVO=new AccountVO();
            accountVO.setUsername(account.getUsername());
            accountVO.setPassword("");
            accountVO.setEmail(account.getEmail());
            accountVO.setFirstName(account.getFirstName());
            accountVO.setLastName(account.getLastName());
            accountVO.setStatus(account.getStatus());
            accountVO.setAddress1(account.getAddress1());
            accountVO.setAddress2(account.getAddress2());
            accountVO.setCity(account.getCity());
            accountVO.setState(account.getState());
            accountVO.setCountry(account.getCountry());
            accountVO.setZip(account.getZip());
            accountVO.setPhone(account.getPhone());

            accountVO.setLanguagePreference(profile.getLanguagePreference());
            accountVO.setBannerOption(profile.isBannerOption());
            accountVO.setListOption(profile.isListOption());
            if(profile.isBannerOption()){
                  accountVO.setFavouriteCategoryId(profile.getFavouriteCategoryId());
                  accountVO.setBannerName(bannerData.getBannerName());
            }else{
                  accountVO.setFavouriteCategoryId("");
                  accountVO.setBannerName("");
            }
            return accountVO;
      }

      private AccountVO entityToVO(Account account){
            AccountVO accountVO=new AccountVO();
            accountVO.setUsername(account.getUsername());
            accountVO.setPassword("");
            accountVO.setEmail(account.getEmail());
            accountVO.setFirstName(account.getFirstName());
            accountVO.setLastName(account.getLastName());
            accountVO.setStatus(account.getStatus());
            accountVO.setAddress1(account.getAddress1());
            accountVO.setAddress2(account.getAddress2());
            accountVO.setCity(account.getCity());
            accountVO.setState(account.getState());
            accountVO.setCountry(account.getCountry());
            accountVO.setZip(account.getZip());
            accountVO.setPhone(account.getPhone());

            accountVO.setLanguagePreference("");
            accountVO.setBannerOption(false);
            accountVO.setListOption(false);

            accountVO.setFavouriteCategoryId("");
            accountVO.setBannerName("");

            return accountVO;
      }


      @Override
      public CommonResponse<AccountVO> registerAccount(Account account, String password,String langpref,String favcategory) {

            String username=account.getUsername();
            Account account1 = accountMapper.selectById(username);
            if (account1!=null){
                  return CommonResponse.createForError("用户名已存在");
            }

            accountMapper.insert(account);
            SignOn signOn=new SignOn();
            signOn.setUsername(username);
            signOn.setPassword(password);
            signOn.setEmail(account.getEmail());
            signOn.setCity(account.getCity());
            signOn.setCountry(account.getCountry());
            signOn.setFirstName(account.getFirstName());
            signOn.setLastName(account.getLastName());
            signOn.setState(account.getState());
            signOn.setPhone(account.getPhone());
            signOn.setZip(account.getZip());
            signOn.setAddress1(account.getAddress1());
            signOnMapper.insert(signOn);
            Profile profile = new Profile();
            profile.setUsername(account.getUsername());
            profile.setFavouriteCategoryId(favcategory);
            profile.setLanguagePreference(langpref);
            profileMapper.insert(profile);


//            QueryWrapper<SignOn> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("username",username)
//            signOnMapper.update(signOn,queryWrapper);

            AccountVO accountVO=entityToVO(account);
            return CommonResponse.createForSuccess(accountVO);
      }

      @Override
      public CommonResponse<AccountVO> editAccount(String username, Account account, SignOn signOn, Profile profile) {

            Account account1 = accountMapper.selectById(username);
            if (account1==null){
                  return CommonResponse.createForError("用户信息不存在");
            }


            QueryWrapper<SignOn> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("username",username);
             signOnMapper.update(signOn,queryWrapper);

            QueryWrapper<Account> queryWrapper1 = new QueryWrapper<>();
            queryWrapper.eq("userid",username);
             accountMapper.update(account,queryWrapper1);

            QueryWrapper<Profile> queryWrapper2 = new QueryWrapper<>();
            queryWrapper.eq("userid",username);
            profileMapper.update(profile,queryWrapper2);


            AccountVO accountVO=entityToVO(account);
            return CommonResponse.createForSuccess(accountVO);
      }

      @Override
      public CommonResponse<SignOn> updateAccount(String username,String password){
            if(accountMapper.selectById(username) == null)
                  return CommonResponse.createForError("用户名不存在");
            else{
                  UpdateWrapper<SignOn> updateWrapper = new UpdateWrapper<>();
                  updateWrapper.eq("username",username);
                  updateWrapper.set("password",password);

                  if(signOnMapper.update(null,updateWrapper) == 1)
                        return CommonResponse.createForSuccessMessage("修改成功");
                  else
                        return CommonResponse.createForError("修改失败");
            }
      }
}
